package com.ruoyi.hyg.service.impl;

import java.util.Date;
import java.util.List;


import com.ruoyi.points.domain.PointsRecord;
import com.ruoyi.points.service.IPointsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hyg.mapper.DailyPracticeMapper;
import com.ruoyi.hyg.domain.DailyPractice;
import com.ruoyi.hyg.service.IDailyPracticeService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DailyPracticeServiceImpl implements IDailyPracticeService {
    @Autowired
    private DailyPracticeMapper dailyPracticeMapper;
    
    @Autowired
    private IPointsRecordService pointsRecordService;
    @Override
    public DailyPractice selectDailyPracticeById(Long id) {
        return dailyPracticeMapper.selectDailyPracticeById(id);
    }
    
    @Override
    public List<DailyPractice> selectDailyPracticeList(DailyPractice dailyPractice) {
        return dailyPracticeMapper.selectDailyPracticeList(dailyPractice);
    }
    
    @Override
    public int insertDailyPractice(DailyPractice dailyPractice) {
        dailyPractice.setCreateTime(DateUtils.getNowDate());
        dailyPractice.setDel(1L);
        return dailyPracticeMapper.insertDailyPractice(dailyPractice);
    }
    
    @Override
    public int updateDailyPractice(DailyPractice dailyPractice) {
        return dailyPracticeMapper.updateDailyPractice(dailyPractice);
    }
    
    @Override
    public int deleteDailyPracticeByIds(Long[] ids) {
        for (Long id : ids) {
            DailyPractice dailyPractice = new DailyPractice();
            dailyPractice.setId(id);
            dailyPractice.setDel(0L);
            dailyPracticeMapper.updateDailyPractice(dailyPractice);
        }
        return ids.length;
    }
    
    @Override
    public int deleteDailyPracticeById(Long id) {
        DailyPractice dailyPractice = new DailyPractice();
        dailyPractice.setId(id);
        dailyPractice.setDel(0L);
        return dailyPracticeMapper.updateDailyPractice(dailyPractice);
    }
    
    @Override
    public DailyPractice selectByUserAndDate(Long userId, Date practiceDate) {
        return dailyPracticeMapper.selectByUserAndDate(userId, practiceDate);
    }
    
    @Override
    public List<DailyPractice> selectDailyRanking(Date practiceDate) {
        return dailyPracticeMapper.selectDailyRanking(practiceDate);
    }
    
    // 新增方法：用于前端展示排行榜
    @Override
    public List<DailyPractice> selectDailyRankingForDisplay(Date practiceDate) {
        return dailyPracticeMapper.selectDailyRankingForDisplay(practiceDate);
    }
    
    // 获取用户累计练习天数
    @Override
    public Integer getUserTotalPracticeDays(Long userId) {
        return dailyPracticeMapper.getUserTotalPracticeDays(userId);
    }
    
    @Override
    public Integer sumQuestionsByUser(Long userId) {
        return dailyPracticeMapper.sumQuestionsByUser(userId);
    }
    
    @Override
    @Transactional
    public void processDailyRankingAndRewards(Date practiceDate) {
        // 获取当天的前三名用户ID和记录
        List<DailyPractice> top3Records = dailyPracticeMapper.selectDailyRanking(practiceDate);
        
        // 为每个前三名用户添加积分记录并更新排名和积分奖励字段
        for (int i = 0; i < top3Records.size(); i++) {
            DailyPractice record = top3Records.get(i);
            Long userId = record.getUserId();
            
            // 更新排名和积分奖励
            record.setRank(i + 1);
            record.setPointsAwarded(20); // 奖励20积分
            dailyPracticeMapper.updateDailyPractice(record);
            
            // 创建积分记录
            PointsRecord pointsRecord = new PointsRecord();
            pointsRecord.setUserId(userId);
            pointsRecord.setType(1);
            pointsRecord.setAmount(20); // 奖励20积分
            pointsRecord.setTitle("每日刷题排名奖励");
            pointsRecord.setDescription("获得" + (i + 1) + "名，奖励20积分");
            
            pointsRecordService.insertPointsRecord(pointsRecord);
        }
    }
}